package cn.retalsystem.service.impl;

import cn.retalsystem.bean.Rental;
import cn.retalsystem.bean.Room;
import cn.retalsystem.dao.RentalMapper;
import cn.retalsystem.dao.RoomMapper;
import cn.retalsystem.service.RoomService;
import cn.retalsystem.util.Cache;
import cn.retalsystem.util.CacheType;
import cn.retalsystem.vo.RoomVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 官宝星
 * @Date: 2019/11/5 23:20
 * @Version 1.0
 * @描述：
 */
@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    RoomMapper roomMapper;
    @Autowired
    RentalMapper rentalMapper;

    @Override
    public Room selectRoomById(Integer id) {
        return roomMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateRoom(Room room) {
        roomMapper.updateByPrimaryKeySelective(room);
    }

    @Override
    public List<RoomVo> selectAllRoomVO() {
        List<RoomVo> vos = roomMapper.selectAllRoomVo();
        List<RoomVo> roomVos=new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (RoomVo vo : vos) {
            Date day = vo.getDay();
            String datString = format.format(day);
            vo.setDayString(datString);
            vo.setMianjiString(vo.getMainji()+"平米");
            roomVos.add(vo);
        }
        return roomVos;
    }

    @Override
    public List<RoomVo> selectRoomByExampleAndPage(String rname, Integer budingId,Integer index,Integer size) {
        PageHelper.startPage(index,size);
        List<RoomVo> vos = roomMapper.selectRoomVoByExample(rname, budingId);
        List<RoomVo> roomVos=new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (RoomVo vo : vos) {
            Date day = vo.getDay();
            String datString = format.format(day);
            vo.setDayString(datString);
            vo.setMianjiString(vo.getMainji()+"平米");
            roomVos.add(vo);
        }
        return roomVos;
    }

    @Override
    public Integer selectCountByExample(String rname, Integer budingId) {
        List<RoomVo> roomVos = roomMapper.selectRoomVoByExample(rname, budingId);
        return roomVos.size();
    }

    @Override
    public Integer selectCount() {
        return roomMapper.selectCount();
    }

    @Override
    public List<RoomVo> selectRoomVoByPage(Integer index, Integer size) {
        PageHelper.startPage(index,size);
        List<RoomVo> vos = roomMapper.selectAllRoomVo();
        List<RoomVo> roomVos=new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (RoomVo vo : vos) {
            Date day = vo.getDay();
            String datString = format.format(day);
            vo.setDayString(datString);
            vo.setMianjiString(vo.getMainji()+"平米");
            roomVos.add(vo);
        }
        return roomVos;
    }

    @Override
    public void delRoomById(List<Integer> ids) {
        for (Integer id : ids) {
            //删除租赁信息
            rentalMapper.deleteRentalByRoomId(id);
            //删除房间信息
            roomMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 添加房间信息，在通过房间id添加租赁信息
     * @param room 房间信息
     */
    @Override
    public void addRoom(Room room) {
        //为room添加创建时间
        Date date = new Date();
        room.setDay(date);
        roomMapper.insertSelective(room);
//        //获取room的id
//        Integer roomId = room.getId();
//        //创建租赁对象
//        Rental rental=new Rental();
//        rental.setDay(date);
//        rental.setAdminid(adminId);
//        rental.setStatus(0);
//        rental.setRoomid(roomId);
//        rentalMapper.insertSelective(rental);
    }


}
